<!DOCTYPE html>
<html>
	<head>
		<title>Tyvo Ranger</title>
		<link rel="stylesheet" type="text/css" title="theme1" href="css/theme1.css"/>
		<script type="text/javascript" src="ajax_like.js"></script>
	</head>
<body>
	<form action="search.php" method="get"> 
		search box : <input name="kataKunci" type="text" />
		<select name="metode">
			<option value="all">No Filter</option>
			<option value="user">User</option>
			<option value="konten">Konten</option>
			<option value="tag">Tag(bkn search)</option>
		</select>
		<input type="submit" value="search" />
	</form>
	<?php 
		if (isset($_GET['kataKunci'])) {
			$mysql_db = "progin_171_13509027";
			$mysql_user = "progin";
			$mysql_pass = "progin";
			$mysql_link = mysql_connect("localhost", $mysql_user, $mysql_pass);
			mysql_select_db($mysql_db, $mysql_link);
			
			$terms = $_GET['kataKunci'];
			
			//bikin list of kata kunci
				function search_split_terms($terms){
					$terms = preg_replace("/\"(.*?)\"/e", "search_transform_term('\$1')", $terms);
					$terms = preg_split("/\s+|,/", $terms);
					$out = array();
					foreach($terms as $term){
						$term = preg_replace("/\{WHITESPACE-([0-9]+)\}/e", "chr(\$1)", $term);
						$term = preg_replace("/\{COMMA\}/", ",", $term);
						$out[] = $term;
					}	
					return $out;
				}	
				function search_transform_term($term){
					$term = preg_replace("/(\s)/e", "'{WHITESPACE-'.ord('\$1').'}'", $term);
					$term = preg_replace("/,/", "{COMMA}", $term);
					return $term;
				}
				function search_escape_rlike($string){
					return preg_replace("/([.\[\]*^\$])/", '\\\$1', $string);
				}
				function search_db_escape_terms($terms){
					$out = array();
					foreach($terms as $term){
						$out[] = '[[:<:]]'.strtoupper(AddSlashes(search_escape_rlike($term))).'[[:>:]]';
					}		
					return $out;
				}
			//selesai bikin list of kata kunci
			$terms = search_split_terms($terms);
			$terms_db = search_db_escape_terms($terms);
			
			$metode=$_GET['metode'];
			$table='undefined';
			
			if (($metode=='user') || ($metode=='all')) {
				$parts = array();
				$table='user';
				$attributes=array('username','nama_lengkap','email','about_me',);
				
				foreach($attributes as $attribute) {
					foreach($terms_db as $term_db){
						$parts[] = "UPPER($attribute) RLIKE '$term_db'";
					}	
				}
				$parts = implode(' OR ', $parts);
				
				$squery = "SELECT * FROM $table WHERE $parts";
				
				//echo"$squery";
				$result = mysql_query($squery);
				$rows = mysql_fetch_array($result);
				$nrow = mysql_numrows($result);
				
				if ($nrow < 1)
					echo "no result found : <br /> (" . $squery .")";
				else {
					echo "found " . $nrow . " result : <br />" . " (" . $squery . ")";
					$i = 0;
					echo"<table border=1px>";
					while ($i < $nrow) {
						$t=array();
						$t[]=mysql_result($result,$i,"username");
						$t[]=mysql_result($result,$i,"email");
						$t[]=mysql_result($result,$i,"avatar");
						$t[]=mysql_result($result,$i,"about_me");
						echo "<tr>";
						foreach ($t as $r) {
							echo"<td><font face='Arial, Helvetica, sans-serif'>$r</font></td>";
						}
						echo"</tr>";
						$i++;
					}
					echo "</table>";
				}
			} 
			if (($metode=='konten') || ($metode=='all')) {
				$parts = array();
				$attributes=array('judul','tag','isi');
				
				foreach($attributes as $attribute) {
					foreach($terms_db as $term_db){
						$parts[] = "UPPER($attribute) RLIKE '$term_db'";
					}	
				}
				$parts = implode(' OR ', $parts);
				
				$squery = "SELECT ID_post, judul, wujud, link, deskripsi, UNIX_TIMESTAMP(waktu) AS waktu
							FROM (
								post
								RIGHT JOIN
								(SELECT DISTINCT id FROM
									(
										(SELECT id, judul, wujud, link, deskripsi, waktu, tag
											FROM (
												(SELECT * FROM post) AS t1 LEFT JOIN
												(SELECT ID_post AS id, tag_name AS tag FROM ditag) AS t2
												ON t1.ID_post=id
											)
										) AS posttag 
										LEFT JOIN 
										(SELECT ID_post, username, isi, waktu
											FROM (
												dikomentar LEFT JOIN
												komentar
												ON dikomentar.ID_komentar=komentar.ID_komentar
											)
										) AS postcomment
									ON id=postcomment.ID_post
									) WHERE $parts
								) 
							AS t3 ON id=post.ID_post)";
							
				$result = mysql_query($squery);
				$rows = mysql_fetch_array($result);
				$nrow = mysql_numrows($result);
				
				if ($nrow < 1)
					echo "<br /><br />no result found : <br /> (" . $squery .")";
				else {
					echo "<br /><br />found " . $nrow . " result : <br />" . " (" . $squery . ")";
					$i = 0;
					echo"<table border=1px>";
					while ($i < $nrow) {
						$t=array();
						$idpost = mysql_result($result,$i,"ID_post");
						$t[]=mysql_result($result,$i,"judul");
						$t[]=mysql_result($result,$i,"wujud");
						$t[]=mysql_result($result,$i,"link");
						$t[]=mysql_result($result,$i,"deskripsi");
						$t[]=mysql_result($result,$i,"waktu");
						
						$taglist = mysql_query("SELECT tag_name FROM ditag WHERE ID_post=$idpost");
						$trows = mysql_fetch_array($taglist);
						$tn = mysql_numrows($taglist);
						
						$commentlist = mysql_query("SELECT t2.ID_komentar, t2.isi, t2.waktu FROM
													(
													(SELECT * FROM dikomentar WHERE ID_post=$idpost) AS t1 LEFT JOIN
													(SELECT * FROM komentar) AS t2 
													ON t1.ID_komentar=t2.ID_komentar
													)");
						$cn = mysql_numrows($commentlist);
						
						echo "<tr>";
						foreach ($t as $r) {
							echo"<td bgcolor='yellow'><font face='Arial, Helvetica, sans-serif'>$r</font></td>";
						}
						echo"</tr><tr><td><font face='Arial, Helvetica, sans-serif'><b>Daftar tag :</b></font></td>";
						$j = 0;
						while ($j < $tn) {
							$tagisi = mysql_result($taglist,$j,"tag_name");
							echo"<td><font face='Arial, Helvetica, sans-serif'>$tagisi</font></td>";
							$j++;
						}
						echo"</tr><tr><td><font face='Arial, Helvetica, sans-serif'><b>Daftar komentar :</b></font></td>";
						$j = 0;
						while ($j < $cn) {
							$cisi = mysql_result($commentlist,$j,"isi");
							echo"<td><font face='Arial, Helvetica, sans-serif'>$cisi</font></td>";
							$j++;
						}
						echo"</tr>";
						$i++;
					}
					echo "</table>";
				}
			}
			if ($metode=='tag') {
				$parts = array();
				$attributes=array('tag');
				
				foreach($attributes as $attribute) {
					foreach($terms_db as $term_db){
						$parts[] = "UPPER($attribute) RLIKE '$term_db'";
					}	
				}
				$parts = implode(' OR ', $parts);
				
				$squery = "SELECT ID_post, judul, wujud, link, deskripsi, UNIX_TIMESTAMP(waktu) AS waktu
							FROM (
								post
								RIGHT JOIN
								(SELECT DISTINCT id FROM
									(
										(SELECT id, judul, wujud, link, deskripsi, waktu, tag
											FROM (
												(SELECT * FROM post) AS t1 LEFT JOIN
												(SELECT ID_post AS id, tag_name AS tag FROM ditag) AS t2
												ON t1.ID_post=id
											)
										) AS posttag 
										LEFT JOIN 
										(SELECT ID_post, username, isi, waktu
											FROM (
												dikomentar LEFT JOIN
												komentar
												ON dikomentar.ID_komentar=komentar.ID_komentar
											)
										) AS postcomment
									ON id=postcomment.ID_post
									) WHERE $parts
								) 
							AS t3 ON id=post.ID_post)";
							
				$result = mysql_query($squery);
				$rows = mysql_fetch_array($result);
				$nrow = mysql_numrows($result);
				
				if ($nrow < 1)
					echo "<br /><br />no result found : <br /> (" . $squery .")";
				else {
					echo "<br /><br />found " . $nrow . " result : <br />" . " (" . $squery . ")";
					$i = 0;
					echo"<table border=1px>";
					while ($i < $nrow) {
						$t=array();
						$idpost = mysql_result($result,$i,"ID_post");
						$t[]=mysql_result($result,$i,"judul");
						$t[]=mysql_result($result,$i,"wujud");
						$t[]=mysql_result($result,$i,"link");
						$t[]=mysql_result($result,$i,"deskripsi");
						$t[]=mysql_result($result,$i,"waktu");
						
						$taglist = mysql_query("SELECT tag_name FROM ditag WHERE ID_post=$idpost");
						$trows = mysql_fetch_array($taglist);
						$tn = mysql_numrows($taglist);
						
						$commentlist = mysql_query("SELECT t2.ID_komentar, t2.isi, t2.waktu FROM
													(
													(SELECT * FROM dikomentar WHERE ID_post=$idpost) AS t1 LEFT JOIN
													(SELECT * FROM komentar) AS t2 
													ON t1.ID_komentar=t2.ID_komentar
													)");
						$cn = mysql_numrows($commentlist);
						
						echo "<tr>";
						foreach ($t as $r) {
							echo"<td bgcolor='yellow'><font face='Arial, Helvetica, sans-serif'>$r</font></td>";
						}
						echo"</tr><tr><td><font face='Arial, Helvetica, sans-serif'><b>Daftar tag :</b></font></td>";
						$j = 0;
						while ($j < $tn) {
							$tagisi = mysql_result($taglist,$j,"tag_name");
							echo"<td><font face='Arial, Helvetica, sans-serif'>$tagisi</font></td>";
							$j++;
						}
						echo"</tr><tr><td><font face='Arial, Helvetica, sans-serif'><b>Daftar komentar :</b></font></td>";
						$j = 0;
						while ($j < $cn) {
							$cisi = mysql_result($commentlist,$j,"isi");
							echo"<td><font face='Arial, Helvetica, sans-serif'>$cisi</font></td>";
							$j++;
						}
						echo"</tr>";
						$i++;
					}
					echo "</table>";
				}
			}
		}
	?>
</body>
</html>